package com.kingdee.pda.ui.activity;

import static com.example.basecommon.utils.LoginUtil.LOGIN_NAME;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.basecommon.http.MyObserver;
import com.example.basecommon.http.RxHelper;
import com.example.basecommon.http.rxjava.BaseResponse;
import com.example.basecommon.http.rxjava.HttpUtils;
import com.example.basecommon.utils.App;
import com.example.basecommon.utils.ConstantUtil;
import com.example.basecommon.utils.PreferencesUtils;
import com.kingdee.pda.Api;
import com.kingdee.pda.R;
import com.kingdee.pda.entity.ConfigQueryResponceBean;
import com.kingdee.pda.entity.PriorityBean;
import com.kingdee.pda.entity.Warehouse;
import com.kingdee.pda.priority.PriorityContract;
import com.kingdee.pda.priority.PriorityPresenter;
import com.kingdee.pda.priority.PriorityUtils;
import com.kingdee.pda.ui.check_order.CheckOrderListActivity;
import com.kingdee.pda.ui.fast_allot.FastAllotOrderListActivity;
import com.kingdee.pda.ui.goods_putaway.GoodsPutawayActivity;
import com.kingdee.pda.ui.inner_replenish.InnerReplenishActivity;
import com.kingdee.pda.ui.interception_registr.InterceptionRegistrActivity;
import com.kingdee.pda.ui.one_click_return.OneClickReturnActivity;
import com.kingdee.pda.ui.out_check.OutCheckActivity;
import com.kingdee.pda.ui.pick_goods.PickGoodsOrderActivity;
import com.kingdee.pda.ui.query_replenish_order.QueryReplenishOrderActivity;
import com.kingdee.pda.ui.replenish_grounding.ReplenishGroundingActivity;
import com.kingdee.pda.ui.scan_inwarehouse.ScanReceiveOrderActivity;
import com.kingdee.pda.ui.scan_outwarehouse.ScanOutWarehouseActivity;
import com.kingdee.pda.ui.sn_query_edit.SnQueryActivity;
import com.kingdee.pda.util.ConstantPreference;
import com.kingdee.pda.util.ToastUtils;

import java.util.HashMap;

/**
 * 主界面
 * Created by zhanlg
 * DATE 2021/5/21
 * TIME 10:59
 */
public class MainActivity extends BaseTitleActivity implements PriorityContract.IPriorityView {

    private ImageView mIvManageArrow;
    private ImageView mIvOperateArrow;
    private ImageView iv_out_operate;
    private ImageView iv_in_operate;

    private LinearLayout mLlWarehouseManage;
    private LinearLayout mLlStockOperate;
    private LinearLayout ll_out_operate;
    private LinearLayout ll_in_operate;

    private LinearLayout ll_goods_query;
    private LinearLayout ll_stock_query;
    private LinearLayout ll_move_in_warehouse;
    private LinearLayout ll_fast_move;
    private LinearLayout ll_replenish_query;
    private LinearLayout ll_replenish_inwarehouse;
    private LinearLayout ll_replenish_grounding;
    private LinearLayout ll_fast_allot;
    private LinearLayout ll_sn_query_edit;
    private LinearLayout ll_check_order;
    private LinearLayout ll_scan_receive;
    private LinearLayout ll_putOn;
    private LinearLayout ll_one_click_return;
    private LinearLayout ll_outWarehouse_check;
    private LinearLayout ll_scan_outWarehouse;
    private LinearLayout ll_interception_registr;
    private LinearLayout ll_pickgoods;
    private PriorityPresenter priorityPresenter;

    private LinearLayout ll_stock_operate_title;
    private LinearLayout ll_warehouse_manage_title;
    private LinearLayout ll_out_operate_title;
    private LinearLayout ll_in_operate_title;


    public static void actionStart(Context context) {
        context.startActivity(new Intent(context, MainActivity.class));
    }

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        super.onActivityCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        priorityPresenter = new PriorityPresenter(this,this);
        String loginName = PreferencesUtils.getString(App.INSTANCE.getApplicationContext(), LOGIN_NAME);
        priorityPresenter.getPriorityList(loginName);
    }

    @Override
    protected void initView() {
        super.initView();
        setBackViewGone();
        setTitle("首页");
        mIvManageArrow = findViewById(R.id.iv_arrow_manage);
        mIvOperateArrow = findViewById(R.id.iv_arrow_operate);
        iv_out_operate = findViewById(R.id.iv_out_operate);
        iv_in_operate = findViewById(R.id.iv_in_operate);

        mLlWarehouseManage = findViewById(R.id.ll_warehouse_manage);
        mLlStockOperate = findViewById(R.id.ll_stock_operate);
        ll_out_operate = findViewById(R.id.ll_out_operate);
        ll_in_operate = findViewById(R.id.ll_in_operate);

        ll_warehouse_manage_title=findViewById(R.id.ll_warehouse_manage_title);
        ll_warehouse_manage_title.setOnClickListener(this);

        ll_stock_operate_title = findViewById(R.id.ll_stock_operate_title);
        ll_stock_operate_title.setOnClickListener(this);

        ll_out_operate_title=findViewById(R.id.ll_out_operate_title);
        ll_out_operate_title.setOnClickListener(this);

        ll_in_operate_title=findViewById(R.id.ll_in_operate_title);
        ll_in_operate_title.setOnClickListener(this);

        ll_stock_query = findViewById(R.id.ll_stock_query);
        ll_stock_query.setOnClickListener(this);

        ll_goods_query = findViewById(R.id.ll_goods_query);
        ll_goods_query.setOnClickListener(this);

        ll_move_in_warehouse = findViewById(R.id.ll_move_in_warehouse);
        ll_move_in_warehouse.setOnClickListener(this);
        ll_fast_move= findViewById(R.id.ll_fast_move);
        ll_fast_move.setOnClickListener(this);
        ll_replenish_query=findViewById(R.id.ll_replenish_query);
        ll_replenish_query.setOnClickListener(this);
        ll_replenish_inwarehouse=findViewById(R.id.ll_replenish_inwarehouse);
        ll_replenish_inwarehouse.setOnClickListener(this);
        ll_replenish_grounding=findViewById(R.id.ll_replenish_grounding);
        ll_replenish_grounding.setOnClickListener(this);
        ll_fast_allot=findViewById(R.id.ll_fast_allot);
        ll_fast_allot.setOnClickListener(this);
        ll_scan_outWarehouse=findViewById(R.id.ll_scan_outWarehouse);
        ll_scan_outWarehouse.setOnClickListener(this);
        ll_outWarehouse_check=findViewById(R.id.ll_outWarehouse_check);
        ll_outWarehouse_check.setOnClickListener(this);
        ll_scan_receive=findViewById(R.id.ll_scan_receive);
        ll_scan_receive.setOnClickListener(this);
        ll_putOn=findViewById(R.id.ll_putOn);ll_putOn.setOnClickListener(this);
        ll_interception_registr=findViewById(R.id.ll_interception_registr);
        ll_interception_registr.setOnClickListener(this);

        ll_one_click_return = findViewById(R.id.ll_one_click_return);
        ll_one_click_return.setOnClickListener(this);
        ll_sn_query_edit=findViewById(R.id.ll_sn_query_edit);
        ll_sn_query_edit.setOnClickListener(this);
        ll_pickgoods=findViewById(R.id.ll_pickgoods);
        ll_pickgoods.setOnClickListener(this);
        ll_check_order=findViewById(R.id.ll_check_order);
        ll_check_order.setOnClickListener(this);

        //只有好太太显示一键退货按钮
        if ("https://www.staryicloud.com/cqhotata".equals(ConstantUtil.getServerUrl()) ||
                "https://uat.staryicloud.com/hotata".equals(ConstantUtil.getServerUrl())) {
            ll_one_click_return.setVisibility(View.VISIBLE);
        } else {
            ll_one_click_return.setVisibility(View.INVISIBLE);
        }

        //查询pda配置
        queryConfig();
    }

    private void queryConfig() {
        HashMap<String, Object> map = new HashMap<>();
        MyObserver<ConfigQueryResponceBean> myObserver = new MyObserver<ConfigQueryResponceBean>(this) {
            @Override
            public void onSuccess(BaseResponse<ConfigQueryResponceBean> result) {
                ConfigQueryResponceBean configQueryResponceBean = result.data;
                PreferencesUtils.putBoolean(MainActivity.this, "autoComplete", configQueryResponceBean.isAutoComplete());
                PreferencesUtils.putBoolean(MainActivity.this, "autoShelves", configQueryResponceBean.isAutoShelves());
                PreferencesUtils.putBoolean(MainActivity.this, ConstantPreference.AUTO_SN, configQueryResponceBean.isAutoSn());
                PreferencesUtils.putBoolean(MainActivity.this, ConstantPreference.INVLIBOUT, configQueryResponceBean.isInvlibout());
            }

            @Override
            public void onFailure(BaseResponse result, String msg) {
                PreferencesUtils.putBoolean(MainActivity.this, "autoComplete", false);
                PreferencesUtils.putBoolean(MainActivity.this, "autoShelves", false);
                PreferencesUtils.putBoolean(MainActivity.this, ConstantPreference.AUTO_SN, false);
                PreferencesUtils.putBoolean(MainActivity.this, ConstantPreference.INVLIBOUT, false);
            }
        };
        HttpUtils.request(Api.class)
                .configQuery(ConstantUtil.CONFIG_QUERY, map)
                .compose(RxHelper.observableIO2Main(this))
                .subscribe(myObserver);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_warehouse_manage_title:
                if (mIvManageArrow.isSelected()) {
                    mIvManageArrow.setSelected(false);
                    mLlWarehouseManage.setVisibility(View.VISIBLE);
                } else {
                    mIvManageArrow.setSelected(true);
                    mLlWarehouseManage.setVisibility(View.GONE);
                }
                break;
            case R.id.ll_stock_operate_title:
                if (mIvOperateArrow.isSelected()) {
                    mIvOperateArrow.setSelected(false);
                    mLlStockOperate.setVisibility(View.VISIBLE);
                } else {
                    mIvOperateArrow.setSelected(true);
                    mLlStockOperate.setVisibility(View.GONE);
                }
                break;
            case R.id.ll_out_operate_title:
                if (iv_out_operate.isSelected()) {
                    iv_out_operate.setSelected(false);
                    ll_out_operate.setVisibility(View.VISIBLE);
                } else {
                    iv_out_operate.setSelected(true);
                    ll_out_operate.setVisibility(View.GONE);
                }
                break;
            case R.id.ll_in_operate_title:
                if (iv_in_operate.isSelected()) {
                    iv_in_operate.setSelected(false);
                    ll_in_operate.setVisibility(View.VISIBLE);
                } else {
                    iv_in_operate.setSelected(true);
                    ll_in_operate.setVisibility(View.GONE);
                }
                break;
            case R.id.ll_stock_query://库位库存查询
                QueryLocationStockActivity.actionStart(this);
                break;
            case R.id.ll_goods_query://商品库位查询
                QueryGoodsLocationActivity.actionStart(this);
                break;
            case R.id.ll_move_in_warehouse://库内移动
                MoveInWarehouseActivity.actionStart(this);
                break;
            case R.id.ll_fast_move://快速移库
                FastMoveOperateActivity.actionStart(this);
                break;
            case R.id.ll_replenish_query://补货查询
                QueryReplenishOrderActivity.actionStart(this);
                break;
            case R.id.ll_replenish_inwarehouse://库内补货
                InnerReplenishActivity.actionStart(this);
                break;
            case R.id.ll_replenish_grounding://补货上架
                ReplenishGroundingActivity.actionStart(this);
                break;
            case R.id.ll_fast_allot://快速调拨
                FastAllotOrderListActivity.actionStart(this);
                break;
            case R.id.ll_scan_outWarehouse://扫描出库
                ScanOutWarehouseActivity.actionStart(this);
                break;
            case R.id.ll_scan_receive://扫描收货
                ScanReceiveOrderActivity.actionStart(this);
                break;
            case R.id.ll_outWarehouse_check://出库复核
                OutCheckActivity.actionStart(this);
                break;
            case R.id.ll_one_click_return://一键退货
                OneClickReturnActivity.actionStart(this);
                break;
            case R.id.ll_sn_query_edit://sn查询编辑
                SnQueryActivity.actionStart(this);
                break;
            case R.id.ll_putOn://商品上架
                GoodsPutawayActivity.actionStart(this);
                break;
            case R.id.ll_interception_registr:
                InterceptionRegistrActivity.actionStart(this);
                break;
            case R.id.ll_pickgoods:
                PickGoodsOrderActivity.actionStart(this);
                break;
            case R.id.ll_check_order:
                CheckOrderListActivity.actionStart(this);
                break;
        }
    }


    @Override
    public void changeWarehouse(Warehouse warehouse) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        updateWarehouseName();
    }

    @Override
    public void getPrioritySuccess(PriorityBean.Data priorityBean) {
        updateView(priorityBean);
    }

    @Override
    public void getPriorityFailed(String msg) {
        ToastUtils.show(this,"获取权限失败，请重试", Toast.LENGTH_LONG);
    }
    private void updateView(PriorityBean.Data priority){
        if (priority == null){
            ToastUtils.show(this,"数据异常，请重试",Toast.LENGTH_SHORT);
            return;
        }
        // 库存管理
        if (priority.stock != null && priority.stock.size() > 0 ){
            ll_warehouse_manage_title.setVisibility(View.VISIBLE);
            for( PriorityBean.Data.ModulePriority stock :priority.stock){
                if (TextUtils.equals(stock.number, "commoditylocation")) {
                    ll_goods_query.setVisibility(View.VISIBLE);
                }
                if (TextUtils.equals(stock.number,"locationstock")) {
                    ll_stock_query.setVisibility(View.VISIBLE);
                }
            }
        }
        //库内管理
        if (priority.insidestore != null && priority.insidestore.size() > 0){
            ll_stock_operate_title.setVisibility(View.VISIBLE);
            for (PriorityBean.Data.ModulePriority stock: priority.insidestore){
                if (TextUtils.equals(stock.number,"replenishmentquery")) {
                    // 补货查询
                    ll_replenish_query.setVisibility(View.VISIBLE);
                    if (stock.function != null && stock.function.size() > 0){
                        for (PriorityBean.Data.ModulePriority.Function function : stock.function){
                            // 开始补货
                            if (TextUtils.equals(function.number,"startreplenishment")){
                                PriorityUtils.INSTANCE.setStartreplenishment(true);
                            }
                        }
                    }

                }
                if(TextUtils.equals(stock.number,"moveinlibrary")) {
                    ll_move_in_warehouse.setVisibility(View.VISIBLE);
                }
                if(TextUtils.equals(stock.number,"replenishmentinwarehouse")){
                    ll_replenish_inwarehouse.setVisibility(View.VISIBLE);
                }
                if(TextUtils.equals(stock.number,"replenishmentandshelving")){
                    ll_replenish_grounding.setVisibility(View.VISIBLE);
                }

                if(TextUtils.equals(stock.number,"quickallocation")){
                    ll_fast_allot.setVisibility(View.VISIBLE);
                    // TODO 创建调拨单
                    if (stock.function != null && stock.function.size() > 0){
                        for (PriorityBean.Data.ModulePriority.Function function : stock.function){
                            // 开始补货
                            if (TextUtils.equals(function.number , "createtransferdoc")){
                                PriorityUtils.INSTANCE.setCreatetransferdoc(true);
                            }
                        }
                    }
                }
                if(TextUtils.equals(stock.number,"SNqueryediting")){
                    ll_sn_query_edit.setVisibility(View.VISIBLE);
                }

                if(TextUtils.equals(stock.number,"quicklibrarytransfer")){
                    ll_fast_move.setVisibility(View.VISIBLE);
                }

                ll_check_order.setVisibility(View.VISIBLE);
                if(TextUtils.equals(stock.number,"stockinventory")){
                    ll_check_order.setVisibility(View.VISIBLE);
                }

            }
        }


        //入库管理

        if (priority.instore != null && priority.instore.size() > 0) {
            ll_in_operate_title.setVisibility(View.VISIBLE);
            for (PriorityBean.Data.ModulePriority stock: priority.instore) {
                if (TextUtils.equals(stock.number, "scanreceipt")){
                    ll_scan_receive.setVisibility(View.VISIBLE);
                }

                if (TextUtils.equals(stock.number, "goodsonshelves")){
                    ll_putOn.setVisibility(View.VISIBLE);
                }

                if (TextUtils.equals(stock.number, "oneclickreturn")){
                    ll_one_click_return.setVisibility(View.VISIBLE);
                }
            }
        }

        // 出库管理
        if (priority.outstore != null && priority.outstore.size() > 0) {
            ll_out_operate_title.setVisibility(View.VISIBLE);
            for (PriorityBean.Data.ModulePriority stock: priority.outstore) {
                if (TextUtils.equals(stock.number, "deliveryrecheck")){
                    ll_outWarehouse_check.setVisibility(View.VISIBLE);
                }

                if (TextUtils.equals(stock.number, "scandelivery")){
                    ll_scan_outWarehouse.setVisibility(View.VISIBLE);
                }

                if (TextUtils.equals(stock.number, "Interceptionregistration")){
                    ll_interception_registr.setVisibility(View.VISIBLE);
                }
                if (TextUtils.equals(stock.number, "PDApicking")){
                    ll_pickgoods.setVisibility(View.VISIBLE);
                }
            }
        }
    }
}
